<!-- Created by henian.xu on 2018/2/2. -->

<template>
    <transition name="bounce">
        <div
            :class="['popup message-box',{'tips':$type==='tips'}]"
            v-show="show"
            @click.self="onPopup('cancel')"
        >
            <div
                class="inner"
                v-if="$type==='tips'">
                <div class="body">{{ message }}</div>
            </div>
            <div
                class="inner"
                v-else>
                <div
                    class="header"
                    v-if="title">
                    <div class="label">{{ title }}</div>
                </div>
                <div :class="['body',{'flex':$type==='alert'||$type==='confirm'}]">
                    <div
                        class="message"
                        v-html="message"/>
                    <x-input
                        v-if="showInput"
                        v-model="inputValue"
                        type="textarea"
                        :placeholder="placeholder"
                        :auto-size="inputAutoSize"

                    />
                    <div
                        class="error"
                        v-if="errorText"
                        v-html="errorText"/>
                </div>
                <div class="footer">
                    <div
                        :class="['btn fill',cancelBtnCls]"
                        v-if="showCancelBtn"
                        @click="handleAction('cancel')"
                        @keydown.enter="handleAction('cancel')"
                    >{{ cancelBtnText }}
                    </div>
                    <div
                        :class="['btn fill',confirmBtnCls]"
                        v-if="showConfirmBtn"
                        @click="handleAction('confirm')"
                        @keydown.enter="handleAction('confirm')"
                    >{{ confirmBtnText }}{{ delayConfirm>0?`(${delayConfirm})`:'' }}
                    </div>
                </div>
                <div
                    class="close-btn"
                    @click="handleAction('close')"
                ><i class="f-icon">&#xf05c;</i></div>
            </div>
        </div>
    </transition>
</template>

<script>
export default {
    name: 'MessageBox',
    data() {
        return {
            show: false,
            showCancelBtn: false,
            cancelBtnText: '取消',
            showConfirmBtn: true,
            confirmBtnText: '确认',

            errorText: '',

            showInput: false,
            inputValue: '',
            placeholder: '请输入',
            inputAutoSize: { maxRows: 4 },

            closeTime: 2000,
            closeTimer: 0,

            delayConfirm: 0,
            delayConfirmTimer: 0,

            isClickOtherClose: true,
            validCallback: null,

            action: '',
            showCloseBtn: false,
            callback: null,
        };
    },
    computed: {
        cancelBtnCls() {
            return 'btn-second';
        },
        confirmBtnCls() {
            if (this.delayConfirm) {
                return 'btn-g5';
            }
            return 'btn-main';
        },
    },
    watch: {
        show(val) {
            if (this.$type === 'tips') {
                if (val) {
                    this.closeTimer = setTimeout(() => {
                        this.action = 'close';
                        this.doClose();
                    }, this.closeTime);
                } else {
                    clearTimeout(this.closeTimer);
                    this.closeTimer = 0;
                }
            } else if (this.delayConfirm > 0) {
                if (val) {
                    this.delayConfirmTimer = setInterval(() => {
                        this.delayConfirm--;
                        if (this.delayConfirm <= 0) {
                            clearInterval(this.delayConfirmTimer);
                            this.delayConfirmTimer = 0;
                        }
                    }, 1000);
                } else {
                    clearInterval(this.delayConfirmTimer);
                    this.delayConfirmTimer = 0;
                }
            }
        },
    },
    methods: {
        onPopup(action) {
            if (!this.isClickOtherClose) return;
            this.handleAction(action);
        },
        validateAction(action) {
            this.errorText = '';
            return new Promise(resolve => {
                if (!this.validCallback) return resolve(true);
                this.$nprogress.start();
                this.validCallback(action)
                    .then(val => {
                        this.$nprogress.done();
                        return resolve(val);
                    })
                    .catch(_err => {
                        this.$nprogress.done();
                        this.errorText = _err.msg;
                    });
            });
        },
        handleAction(action) {
            this.validateAction(action).then(() => {
                if (action === 'confirm' && this.delayConfirm > 0) return;
                this.action = action;
                this.doClose();
            });
        },
        doClose() {
            this.show = false;
            this.callback(this.action, this);
        },
    },
};
</script>

<style lang="scss" scoped>
@import '~/assets/style/_index.scss';
.popup-wrap {
    > .popup {
        &.message-box {
            z-index: $z-index-8;
        }
    }
}

.popup.message-box {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    .inner {
        position: relative;
        min-width: 300px;
        max-width: 90%;
        min-height: 260px;
        max-height: 80%;
        width: auto;
        background-color: #fff;
        border-radius: 5px;
        overflow: hidden;

        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: stretch;

        > .header,
        > .footer {
            min-height: 1rem;
            flex: 0 0 auto;
            line-height: 50px;
        }
        > .header {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: stretch;
            border-bottom: 1px solid $color-border;
            padding: 0 $padding-small;

            > .label {
                flex: 1 1 auto;
                text-align: center;
                color: $color-main;
            }
            > .btn {
                border-radius: 0;
                border: 0;
            }
            > * {
                flex: 0 0 auto;
            }
        }
        > .footer {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: stretch;
            text-align: center;
            //border-top: 1px solid $color-border;

            > .btn {
                flex: 1 1 auto;
                width: 1%;
                line-height: 40px;
            }
        }
        > .body {
            flex: 1 1 auto;
            padding: $padding;
            overflow-x: hidden;
            overflow-y: auto;
            -webkit-overflow-scrolling: touch;
        }

        > .body.flex {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            padding: $padding-big * 1.5;
        }
        > .close-btn {
            display: flex;
            flex-direction: row;
            justify-content: center;
            align-items: center;
            color: $gray6;
            cursor: pointer;

            position: absolute;
            top: 0px;
            right: 0px;

            width: 50px;
            height: 50px;
            font-size: 26px;
        }
    }

    &.tips {
        background-color: rgba(0, 0, 0, 0);
        > .inner {
            width: auto;
            min-width: 50vw;
            max-width: 70vw;
            min-height: auto;
            background-color: rgba(0, 0, 0, 0.7);
            color: #fff;
            > .body {
                text-align: center;
            }
        }
    }
}
</style>
